<template>
  <div class="container">
    <CommentList v-if="userInfo.openId" type="user" :comments="comments"></CommentList>
    <div class="div" v-if="userInfo.openId">
      <div class="title">我的图书</div>
      <Card v-for="book in books" :book="book" :key="book.id"></Card>
      <div v-if="!books.length">没有图书，快去添加</div>
    </div>
  </div>
</template>
<script>
import {get} from '@/utils'
import CommentList from '@/components/CommentList'
import Card from '@/components/Card'

export default {
  data () {
    return {
      comments: [],
      books: [],
      userInfo: {}
    }
  },
  components: {
    CommentList,
    Card
  },
  methods: {
    init () {
      this.getComment()
      this.getBooks()
    },
    async getComment () {
      let comments = await get('/weapp/commentlist', {openid: this.userInfo.openId})
      this.comments = comments.data.list
      // console.log(comments.data.list)
    },
    async getBooks () {
      let books = await get('/weapp/booklist', {openid: this.userInfo.openId})
      this.books = books.data.list
      console.log(books.data.list)
    }
  },
  onPullDownRefresh () {
    this.init()
    wx.stopPullDownRefresh()
  },
  onShow () {
    if (!this.userInfo.openid) {
      const userInfo = wx.getStorageSync('userInfo')
      if (userInfo) {
        this.userInfo = userInfo
        this.init()
      }
    }
  }
}
</script>
<style>

</style>


